﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IService;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service
{
    public class EquipmentMaintenanceService : BaseService, IEquipmentMaintenanceService
    {
        private readonly IEquipmentMaintenanceRepository _EquipmentMaintenanceRepository;
        private readonly IUnitOfWork _unitOfWork;
        public EquipmentMaintenanceService(IMapper mapper, IEquipmentMaintenanceRepository _EquipmentMaintenanceRepository, IUnitOfWork _unitOfWork) : base(mapper)
        {
            this._EquipmentMaintenanceRepository = _EquipmentMaintenanceRepository;
            this._unitOfWork = _unitOfWork;
        }

        public bool AddEquipmentMaintenance(EquipmentMaintenance EquipmentMaintenance)
        {
            _EquipmentMaintenanceRepository.AddEntity(EquipmentMaintenance);
            return _unitOfWork.SaveChangesEntity();
        }

        public bool DelEquipmentMaintenance(long id)
        {
            var cust = _EquipmentMaintenanceRepository.QueryEntityById(id);
            if (cust !=null)
            {
                _EquipmentMaintenanceRepository.DelEntity(cust);
                return _unitOfWork.SaveChangesEntity();
            }
            return false;
        }

        public List<EquipmentMaintenance> GetEquipmentMaintenance(string? name, int pageIndex, int pageSize, out int totalCount)
        {
            var custList = _EquipmentMaintenanceRepository.GetEntityAlls();
            if (!string.IsNullOrEmpty(name))
            {
                custList = custList.Where(c => c.Name.Contains(name));
            }
            var list = _EquipmentMaintenanceRepository.GetEntityAlls(custList, pageIndex, pageSize, out totalCount, x => x.CreateDate, false);
            return list.ToList();
        }

        public EquipmentMaintenance? GetEquipmentMaintenanceById(long id)
        {
            return _EquipmentMaintenanceRepository.QueryEntityById(id);
        }

        public bool UpdEquipmentMaintenance(EquipmentMaintenance EquipmentMaintenance)
        {
            var aobj = _EquipmentMaintenanceRepository.QueryIsEntityWhere(x => x.Id == EquipmentMaintenance.Id);
            if (aobj == null)
            {
                return false;
            }
            _EquipmentMaintenanceRepository.UpdEntity(EquipmentMaintenance);
            return _unitOfWork.SaveChangesEntity();
        }
    }
}
